import {Game,MeshTool,Input} from "./game.js"

class Player{
    constructor(){
        this.pos = new THREE.Vector3(0,0,0);
    }
}

export class FirstPersonShoot{
    constructor(camera,scene,renderer){
        this.camera = camera;
        this.scene = scene;
        this.renderer = renderer;
        this.domElement = document;
        this.mouse = new THREE.Vector2();
        this.raycaster = new THREE.Raycaster();
        this.pos = new THREE.Vector3(0,0,0);
        this.init();
    }
    
    init(){
        this.cube = MeshTool.makeCube(new THREE.Vector3(1,1,3),new THREE.Vector3(0,0,0.5));
        this.scene.add(this.cube);
        
        var _onMouseMove = bind( this, this.onMouseMove );
        var _onMouseDown = bind( this, this.onMouseDown );
        var _onMouseUp = bind( this, this.onMouseUp );
        var _onKeyDown = bind( this, this.onKeyDown );
        var _onKeyUp = bind( this, this.onKeyUp );
        
        function contextmenu( event ) {
            event.preventDefault();
        }

        this.domElement.addEventListener( 'contextmenu', contextmenu, false );
        this.domElement.addEventListener( 'mousemove', _onMouseMove, false );
        this.domElement.addEventListener( 'mousedown', _onMouseDown, false );
        this.domElement.addEventListener( 'mouseup', _onMouseUp, false );

        window.addEventListener( 'keydown', _onKeyDown, false );
        window.addEventListener( 'keyup', _onKeyUp, false );

        function bind( scope, fn ) {
            return function () {
                fn.apply( scope, arguments );
            };
        }
    }
    
    move(pos){
        this.raycaster.set( pos, new THREE.Vector3(pos.x,pos.y,-10));
        let ret=this.raycaster.intersectObjects(this.scene.children,true);
        if(ret.length>0)
        {
            this.cube.position.copy(ret[0].point);
        }
    }
    
    onMouseDown( event ) {

		if ( this.domElement !== document ) {

			this.domElement.focus();

		}

		event.preventDefault();
		event.stopPropagation();

		if ( this.activeLook ) {

			switch ( event.button ) {

				case 0: this.moveForward = true; break;
				case 2: this.moveBackward = true; break;

			}

		}

		this.mouseDragOn = true;

	};

	onMouseUp( event ) {

		event.preventDefault();
		event.stopPropagation();

		if ( this.activeLook ) {
			switch ( event.button ) {
				case 0: this.moveForward = false; break;
				case 2: this.moveBackward = false; break;
			}
		}
		this.mouseDragOn = false;

	};

	onMouseMove( event ) {
		if ( this.domElement === document ) {
			this.mouseX = event.pageX - this.viewHalfX;
			this.mouseY = event.pageY - this.viewHalfY;
		} else {
			this.mouseX = event.pageX - this.domElement.offsetLeft - this.viewHalfX;
			this.mouseY = event.pageY - this.domElement.offsetTop - this.viewHalfY;
		}
        
        /*
        let x = ( event.clientX / this.renderer.domElement.clientWidth ) * 2 - 1;
        let y = - ( event.clientY / this.renderer.domElement.clientHeight ) * 2 + 1;
        //this.mouse.x = ( event.clientX / this.renderer.domElement.clientWidth ) * 2 - 1;
		//this.mouse.y = - ( event.clientY / this.renderer.domElement.clientHeight ) * 2 + 1;
		//this.raycaster.setFromCamera( this.mouse, this.camera );
        this.raycaster.set( new THREE.Vector3(x,y+10,1), new THREE.Vector3(x,y,-1));
        let ret=this.raycaster.intersectObjects(this.scene.children,true);
        if(ret.length>0)
        {
            this.cube.position.copy(ret[0].point);
        }
        */
	};
    
    onKeyDown( event ) {
		//event.preventDefault();
        
        Input.setKey(event.keyCode,true);
		switch ( event.keyCode ) {
			case 38: /*up*/
			case 87: /*W*/ {
                this.moveForward = true;
                
                this.pos.y+=10;
                this.move(this.pos);
                
                break;
            }
			case 37: /*left*/
			case 65: /*A*/ this.moveLeft = true; break;
			case 40: /*down*/
			case 83: /*S*/ {
                this.moveBackward = true;
                
                this.pos.y-=10;
                this.move(this.pos);
                
                break;
            }
			case 39: /*right*/
			case 68: /*D*/ this.moveRight = true; break;
			case 82: /*R*/ this.moveUp = true; break;
			case 70: /*F*/ this.moveDown = true; break;
		}
	}

	onKeyUp( event ) {
        Input.setKey(event.keyCode,false);
		switch ( event.keyCode ) {
			case 38: /*up*/
			case 87: /*W*/ {
                this.camera.rotation.z+=Math.PI / 8;
                this.moveForward = false; 
                break;
            }
			case 37: /*left*/
			case 65: /*A*/ this.moveLeft = false; break;
			case 40: /*down*/
			case 83: /*S*/ {
                this.camera.rotation.z-=Math.PI / 8;
                this.moveBackward = false; 
                break;
            }
			case 39: /*right*/
			case 68: /*D*/ this.moveRight = false; break;
			case 82: /*R*/ this.moveUp = false; break;
			case 70: /*F*/ this.moveDown = false; break;
		}

	}
    
    update(){
        /*this.camera.position.y=-10;
        this.camera.position.z=2;
        this.camera.rotation.x=Math.PI/2;*/
    }
}